package com.itheima.test3;

import java.util.ArrayList;
import java.util.Collections;
import java.util.concurrent.Callable;

public class MyCallable implements Callable<Integer> {
    //创建集合
    ArrayList<Integer> list;
    //构造方法
    public MyCallable(ArrayList<Integer> list) {
        this.list = list;
    }

    @Override
    public Integer call() throws Exception {
        //循环
        //同步代码块
        //判断已经达到了末尾
        //判断没有达到末尾
        //创建一个内部的集合
        ArrayList<Integer> demo =new ArrayList<>();
        while (true){
            synchronized (MyCallable.class){
                if(list.size() == 0){
                    System.out.println(Thread.currentThread().getName() + demo);
                    break;
                }else{
                    //进行抽奖
                    /*
                    方法1：进行打乱，然后获取0索引的值
                    方法2：利用Random进行抽取
                     */
                    Collections.shuffle(list);
                    Integer result = list.get(0);
                    list.remove(result);
                    //System.out.println(getName() + "抽取到了" + result +"元大奖");
                    demo.add(result);
                }
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        if(demo.size() == 0){
            return null;
        }else{
            return Collections.max(demo);
        }
    }
}
